package com.msc.mc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;

public class NIOMaterialImport {
	
	public static void main( String[] args ) throws Exception {
		NIOMaterialImport ddImport = new NIOMaterialImport( "ABS-C1" , "NIO-NO.XXX.2017-C1" , "1111" , "111" , "2222" , "3333" , "255 255 200" , "1.5" );
		ddImport.createImportZipFile("/data/" );
	}
	
	private long timeSeq = 0 ;
	
	
	public NIOMaterialImport(String materialSymbol, String matSpecNumber, String matId, String classId, String specId,
			String matReqId, String color, String density) {
		super();
		this.materialSymbol = materialSymbol;
		this.matSpecNumber = matSpecNumber;
		this.matId = matId;
		this.classId = classId;
		this.specId = specId;
		this.matReqId = matReqId;
		this.color = color;
		this.density = density;
		
		this.name =  this.materialSymbol  + ";" + this.matSpecNumber;
		
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		timeSeq = Long.parseLong( sdf.format( now ) );
	}

	private String matSpecNumber ;
	private String materialSymbol;
	private String name ;
	private String matId;
	
	private String classId ;
	private String specId ;
	private String matReqId ;
	private String color;
	private String density;
	
	


	public long getTimeSeq() {
		return timeSeq;
	}

	public void setTimeSeq(long timeSeq) {
		this.timeSeq = timeSeq;
	}

	public String getMatSpecNumber() {
		return matSpecNumber;
	}

	public void setMatSpecNumber(String matSpecNumber) {
		this.matSpecNumber = matSpecNumber;
	}

	public String getMaterialSymbol() {
		return materialSymbol;
	}

	public void setMaterialSymbol(String materialSymbol) {
		this.materialSymbol = materialSymbol;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMatId() {
		return matId;
	}

	public void setMatId(String matId) {
		this.matId = matId;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public String getSpecId() {
		return specId;
	}

	public void setSpecId(String specId) {
		this.specId = specId;
	}

	public String getMatReqId() {
		return matReqId;
	}

	public void setMatReqId(String matReqId) {
		this.matReqId = matReqId;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getDensity() {
		return density;
	}

	public void setDensity(String density) {
		this.density = density;
	}

	public String createImportZipFile( String scriptWorkingDir   ) throws Exception {
		SimManagerImportBean importBean = new SimManagerImportBean();
		importBean.addParam("canEditDuringImport", "false");
		importBean.addParam("useOidDuringImport", "false");
		
		ObjectListBean matClass  = importBean.addObjectList( "NIOMatClassification" , "[objectId=='" + classId + "']", true );
		ObjectListBean matSpec   = importBean.addObjectList( "NIOMatSpecification" , "[objectId=='" + specId + "']", true);
		ObjectListBean matReq    = importBean.addObjectList( "NewMaterialRequest" , "[objectId=='" + matReqId + "']", true);
		
		ObjectListBean matSchemal   = importBean.addObjectList( "MatMaterialsSchema" , "[name == 'NIOMaterial']", true );
		
		ObjectListBean colorProp    = importBean.addObjectList( "MatPropertyDef" , "[name == 'color']", true );
		ObjectListBean densityProp  = importBean.addObjectList( "MatPropertyDefMeasure" , "[name == 'density']", true );
		ObjectListBean matSpecProp  = importBean.addObjectList( "MatPropertyDefReference" , "[name == 'matSpec']", true );
		ObjectListBean matClassProp = importBean.addObjectList( "MatPropertyDefReference" , "[name == 'matClassification']", true );
		ObjectListBean matRequest   = importBean.addObjectList( "MatPropertyDefReference" , "[name == 'newMaterialRequest']", true );
		
		 
		ObjectBean dsItem = importBean.addObject( "Item" );
		dsItem.addAttributeBean( "name" , getUniqueName( "DS" ) , "trueAndAutoAdjust" );
		
		ObjectBean dsVariant = importBean.addObject( "Variant" );
		dsVariant.addAttributeBean( "item" , dsItem.getId() , "false" );
		dsVariant.addAttributeBean( "name" , "V1" , "false" );
		
		ObjectBean dsBean = importBean.addObject( "MatDataSource" );
		dsBean.addAttributeBean( "name", getUniqueName( "DS" ) , "false" ); 
		dsBean.addAttributeBean( "schema", matSchemal.getId() , "false" );
		dsBean.addAttributeBean( "variant", dsVariant.getId() , "false" );
		
		
		ObjectBean designDataItem = importBean.addObject( "Item" );
		designDataItem.addAttributeBean( "name" , getUniqueName( name )  ,  "trueAndAutoAdjust" );
		
		ObjectBean designDataVariant = importBean.addObject( "Variant" );
		designDataVariant.addAttributeBean( "item" , designDataItem.getId() ,  "false" );
		designDataVariant.addAttributeBean( "name" , "V1",  "false" );
		designDataVariant.addAttributeBean( "matClass"    , matClass , "false" );
		designDataVariant.addAttributeBean( "matSpec"     , matSpec  , "false" );
		designDataVariant.addAttributeBean( "matRequest"  , matReq  , "false" );
		
	 
		ObjectBean colorDataBean = importBean.addObject( "MatStringProperty" );
		colorDataBean.addAttributeBean( "matPropertyDef", colorProp , "false" );
		colorDataBean.addAttributeBean( "name", "color" , "false" );
		colorDataBean.addAttributeBean( "value", color , "false" );
		colorDataBean.addAttributeBean( "variant", designDataVariant , "false" );
		
		ObjectBean densityDataBean = importBean.addObject( "MatMeasureProperty" );
		densityDataBean.addMeasureBean( "value" , "Nominal" , density , "1"  , "Density" , "g_per_cm__3" );
		densityDataBean.addAttributeBean( "matPropertyDef", densityProp , "false" );
		densityDataBean.addAttributeBean( "name", "density" , "false" );
		densityDataBean.addAttributeBean( "variant", designDataVariant , "false" );
		
		
		ObjectBean specBean = importBean.addObject( "MatReferenceProperty" );
		specBean.addAttributeBean( "matPropertyDef", matSpecProp , "false" );
		specBean.addAttributeBean( "name", "matSpec" , "false" );
		specBean.addAttributeBean( "value", matSpec , "false" );
		specBean.addAttributeBean( "variant", designDataVariant , "false" );
		
		ObjectBean classBean = importBean.addObject( "MatReferenceProperty" );
		classBean.addAttributeBean( "matPropertyDef", matClassProp , "false" );
		classBean.addAttributeBean( "name", "matClassification" , "false" );
		classBean.addAttributeBean( "value", matClass , "false" );
		classBean.addAttributeBean( "variant", designDataVariant , "false" );
		
		ObjectBean requestBean = importBean.addObject( "MatReferenceProperty" );
		requestBean.addAttributeBean( "matPropertyDef", matRequest , "false" );
		requestBean.addAttributeBean( "name", "newMaterialRequest" , "false" );
		requestBean.addAttributeBean( "value", matReq , "false" );
		requestBean.addAttributeBean( "variant", designDataVariant , "false" );
		
		
		ObjectBean designDataBean = importBean.addObject( "NIOMaterial" );
		designDataBean.addAttributeBean("name", name , "false" );
		designDataBean.addAttributeBean("matSpecNumber",matSpecNumber, "false" );
		designDataBean.addAttributeBean("materialSymbol", materialSymbol , "false" );
		designDataBean.addAttributeBean("matId", matId , "false" );
		designDataBean.addAttributeBean("description","New NIO Design Data", "false" );
		designDataBean.addAttributeBean("variant" , designDataVariant.getId() , "false" );
		designDataBean.addAttributeBean("matRevisableProperties", new ImportBean[]{ colorDataBean , densityDataBean , specBean , classBean ,  requestBean  }   , "false" );//TODO
		designDataBean.addAttributeBean("generatedFromModel",  dsBean   , "false" );
		
		
		String message = importBean.getXMLString();
		File xmlFile   = importBean.getXMLFile( scriptWorkingDir , "structure.xml" );
System.out.println( message );


		String zipFilePath = scriptWorkingDir + "MaterialEditor.zip" ;
		FileOutputStream fos=new java.io.FileOutputStream(zipFilePath); 
		ArchiveOutputStream zaos = new org.apache.commons.compress.archivers.ArchiveStreamFactory().createArchiveOutputStream("zip",new java.io.FileOutputStream(zipFilePath) );
		 
		ZipArchiveEntry zipArchiveEntry = new org.apache.commons.compress.archivers.zip.ZipArchiveEntry( xmlFile.getName() );    
		//添加成员到压缩包对象中    
		zaos.putArchiveEntry(zipArchiveEntry);     
		//获取当前文件的输入流    
		FileInputStream fis = new java.io.FileInputStream( xmlFile );
		//将输入流内容读取到压缩包输出流中    
		org.apache.commons.compress.utils.IOUtils.copy( fis , zaos);     
		//清除entry对象准备添加下一个文件   
		zaos.closeArchiveEntry();    
		fis.close(); 
		zaos.finish();
		fos.close();
		return zipFilePath ;
	}
	
	public  String getUniqueName( String prefix ) {
		//return prefix + CoreWidgetsUtils.generateUnqiueWidgetName();
		return prefix + Long.toHexString( timeSeq++ ) ;
	}
	
}
